
//var API_KEY = "=hzfzbfozMkjRQvH3kdEzHtO208="
var API_KEY = "HVczX3vGKw7YCcKrZ2CmlYlaSI4="

function getDeviceStatus(device_id) {
  var deviceConnected
  //查看设备连接状态，并刷新按钮状态
  wx.request({
    url: "https://api.heclouds.com/devices/" + device_id,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
      "api-key": API_KEY
    },
    data: {
      temp1:0,
      temp2:0,
    },
    success(res) {
      console.log(res)
      if (res.data.data.online) {
        console.log("设备已经连接")
        deviceConnected = true
        return true
      } else {
        console.log("设备还未连接")
        deviceConnected = false
        return false
      }
    },
    fail(res) {
      console.log("请求失败")
      deviceConnected = false
      return false
    }
  })
}


function post_switch(choice){
  var that = this
  var strName = "Songtu";
  var value = "0";
  if(choice==1){
    strName="Songtu";
    value=0;
  }
  else if (choice==2){
    strName = "Songtu";
    value = 111;
  }
  else if (choice == 3) {
    strName = "Jiaoshui";
    value = 0;
  }
  else if (choice == 4) {
    strName = "Jiaoshui";
    value = 111;
  }
  else{
    console.log("数据内部错误！");
  }
  let deviceid = "608581212"
  let apikey = "HVczX3vGKw7YCcKrZ2CmlYlaSI4="
  let data = {
    "datastreams": [
      // { "temp": "35", "humi": 89, "light":99, "Songtu": 111, "Jiaoshui":111 }
      { "id": strName, "datapoints": [{ "value": value }] },
    ]
  }
  wx.request({
    url: "https://api.heclouds.com/devices/" + deviceid + "/datapoints",
    method: 'POST',
    header: {
      "content-type": 'application/json',
      "api-key": apikey
    },
    data: JSON.stringify(data),
    success(res) {
      console.log("更新数据成功", res)
    },
    fail: function (res) {
      wx.showToast({ title: '系统错误' })
    },
    complete: function (res) {
      wx.hideLoading()
    }
  })
}



module.exports = {
  getDeviceStatus,
  post_switch
  
} 